package cn.jwcat.service.impl;

import cn.jwcat.dealEntity.ClaimDto;
import cn.jwcat.dealEntity.DealClaim;
import cn.jwcat.entity.*;
import cn.jwcat.mapper.ClaimMapper;
import cn.jwcat.service.IClaimService;
import cn.jwcat.service.IGoodsService;
import cn.jwcat.service.IUserService;
import cn.jwcat.utils.JwtTokenUtil;
import cn.jwcat.utils.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class ClaimServiceImpl extends ServiceImpl<ClaimMapper, Claim> implements IClaimService {

	@Resource
	IGoodsService goodsService;

	@Resource
	IUserService userService;

	@Resource
	JwtTokenUtil jwtTokenUtil;

	@Override
	public List<ClaimDto> getAll() {
		MPJLambdaWrapper<Claim> wrapper = baseWrapper();
		return this.getBaseMapper().selectJoinList(ClaimDto.class, wrapper);
	}

	@Override
	public Boolean putDealClaim(DealClaim dealClaim) {
		if (dealClaim.getStatus().equals("同意")) {
			// 数量操作
			Goods goods = goodsService
				.getBaseMapper()
				.selectById(dealClaim.getGoodsId());
			LambdaUpdateWrapper<Goods> wrapper = new LambdaUpdateWrapper<>();
			wrapper
				.set(Goods::getNumber, goods.getNumber() - dealClaim.getClaimNumber())
				.eq(Goods::getGoodsId, dealClaim.getGoodsId());
			goodsService.getBaseMapper().update(null, wrapper);
		}
		// 添加操作
		LambdaUpdateWrapper<Claim> updateWrapper = new LambdaUpdateWrapper<>();
		updateWrapper
			.set(Claim::getClaimNumber, dealClaim.getClaimNumber())
			.set(Claim::getStatus, dealClaim.getStatus())
			.set(Claim::getReply, dealClaim.getReply())
			.eq(Claim::getClaimId, dealClaim.getClaimId());
		int updateClaim = this.getBaseMapper().update(null, updateWrapper);
		return updateClaim == 1;
	}

	@Override
	public List<ClaimDto> queryByStatus(DealClaim dealClaim) {
		MPJLambdaWrapper<Claim> wrapper = baseWrapper();
		wrapper.eq(Claim::getStatus, dealClaim.getStatus());
		return this.getBaseMapper().selectJoinList(ClaimDto.class, wrapper);
	}

	@Override
	public Result postClaimDto(ClaimDto claimDto, Staff staff) {
		String uname = claimDto.getUname();
		LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(User::getUname, uname).eq(User::getStatus, true);
		User user = userService.getOne(wrapper);
		if (user == null) return Result.error().message("用户未找到，请检查输入内容");
		Claim claim = new Claim();
		claim.setGoodsId(claimDto.getGoodsId()).setClaimNumber(claimDto.getClaimNumber())
			.setUserId(user.getUserId()).setRemark(claimDto.getRemark()).setVolunteerId(staff.getStaffId());
		boolean save = this.save(claim);
		if (save) return Result.success("物资申请成功，请耐心等待！");
		return Result.error();
	}

	@Override
	public List<ClaimDto> getClaim(Integer staffId) {
		MPJLambdaWrapper<Claim> wrapper = baseWrapper();
		wrapper.eq(Claim::getVolunteerId, staffId);
		return this.getBaseMapper().selectJoinList(ClaimDto.class, wrapper);
	}

	@Override
	public List<ClaimDto> getPhone(Integer id) {
		MPJLambdaWrapper<Claim> wrapper = baseWrapper();
		wrapper.eq(User::getUserId, id);
		return this.getBaseMapper().selectJoinList(ClaimDto.class, wrapper);
	}

	public MPJLambdaWrapper<Claim> baseWrapper() {
		MPJLambdaWrapper<Claim> wrapper = new MPJLambdaWrapper<>();
		return wrapper.selectAll(Claim.class).select(Staff::getUsername)
			.select(User::getUname).select(Goods::getNumber, Goods::getGoodsName,
				Goods::getPicture, Goods::getSpecification, Goods::getGoodsId).select(Room::getName)
			.leftJoin(Goods.class, Goods::getGoodsId, Claim::getGoodsId)
			.leftJoin(User.class, User::getUserId, Claim::getUserId)
			.leftJoin(Room.class, Room::getRoomId, User::getRoomId)
			.leftJoin(Staff.class, Staff::getStaffId, Claim::getVolunteerId)
			.orderByDesc(Claim::getCreateTime);
	}
}
